package controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import model.BounBookUser;
import model.PrivateMessage;
import util.HibernateUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PrivateMessageController extends ActionSupport {

	public static final long serialVersionUID = 1L;

	public PrivateMessage privateMessage = new PrivateMessage();
	public List<PrivateMessage> list = new ArrayList<PrivateMessage>();
	public List<Object> userList = new ArrayList<Object>();

	int messageId;
	
	
	public String create() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		int fromUserId = Integer.parseInt(session.get("userId").toString());
		privateMessage.setFromUserId(fromUserId);
		HibernateUtil.insert(privateMessage);
		list = HibernateUtil.list("from PrivateMessage as p where p.fromUserId = \'"+ fromUserId +"\'");

		for (PrivateMessage message : list) {
			message.setUserFullName(HibernateUtil.get(BounBookUser.class, message.getToUserId()).getFullName());
		}
		return SUCCESS;
	}
	
	public String listInbox() {
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		int toUserId = Integer.parseInt(session.get("userId").toString());
		
		list = HibernateUtil.list("from PrivateMessage as p where p.toUserId = \'"+ toUserId +"\'");

		
		
		for (PrivateMessage message : list) {
			message.setUserFullName(HibernateUtil.get(BounBookUser.class, message.getFromUserId()).getFullName());
		}
		
		return SUCCESS;
	}
	
	public PrivateMessage getPrivateMessage() {
		return privateMessage;
	}

	public void setPrivateMessage(PrivateMessage privateMessage) {
		this.privateMessage = privateMessage;
	}

	public List<PrivateMessage> getList() {
		return list;
	}

	public void setList(List<PrivateMessage> list) {
		this.list = list;
	}

	public List<Object> getUserList() {
		return userList;
	}

	public void setUserList(List<Object> userList) {
		this.userList = userList;
	}

	public int getMessageId() {
		return messageId;
	}

	public void setMessageId(int messageId) {
		this.messageId = messageId;
	}

	public String listOutbox() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		int fromUserId = Integer.parseInt(session.get("userId").toString());
		list = HibernateUtil.list("from PrivateMessage as p where p.fromUserId = \'"+ fromUserId +"\'");

		for (PrivateMessage message : list) {
			message.setUserFullName(HibernateUtil.get(BounBookUser.class, message.getToUserId()).getFullName());
		}
		
		return SUCCESS;
	}
	
	public String execute() {
		userList = HibernateUtil.list("from BounBookUser");
		
		return SUCCESS;
	}
	

	public String show() {
		privateMessage = HibernateUtil.get(PrivateMessage.class, messageId);
		
		return SUCCESS;
	}
	
}
